import { reqGetGoodInfo, reqAddOrUpdateShopCart } from "@/api"
import { getUUID } from '@/utils/index';

const state = {
    goodInfo:{},
    //游客临时身份
    uuid_token: getUUID()
}
const actions = {
    async getGoodInfo({commit}, skuId) {
        let res = await reqGetGoodInfo(skuId)
        if(res.code == 200) {
            commit('GETGOODINFO',res.data)
        }
    },
    //加入购物车
    async getAddOrUpdateShopCart({commit}, {skuId, skuNum}) {
        let res = await reqAddOrUpdateShopCart(skuId, skuNum)
        // console.log(res);
        //代表服务器加入购物车成功
        if(res.code == 200) {
            return 'ok'
        } else {
            //代表服务器加入购物车失败
            return Promise.reject(new Error('faile'))
        }
    }
}
const mutations = {
    GETGOODINFO(state,goodInfo) {
        state.goodInfo = goodInfo
    }
}
const getters = {
    categoryView(state) {
        return state.goodInfo.categoryView || {}
    },
    skuInfo(state) {
        return state.goodInfo.skuInfo || {}
    },
    spuSaleAttrList(state) {
        return state.goodInfo.spuSaleAttrList || []
    }
}

export default {
    state,
    actions,
    mutations,
    getters
}